#  Welcome python world
#  It's me Lucas
#  Please start your creation
import pandas as pd
import time
import os
import openpyxl
import qrcode


class Re_port:
    def __init__(self, path):
        self.path1 = path  # sn文件路径
        self.path2 = None

    def get_png(self):
        df = pd.read_excel(self.path1, header=None, names=['col'])  # 读取，默认只有一列
        list_sn = df.col.to_list()
        num = len(list_sn)
        print(fr'文件中共计有{num}条数据')
        try:
            os.mkdir('photo')  # 创建文件夹
        except:
            print('二维码储存于photo文件夹中')
        self.path2 = os.path.abspath('photo')
        print(self.path2)

        for i in list_sn:
            if i:
                png = qrcode.make(f"{i}")
                png.save(fr"./photo/{i}.png")

    def get_excel(self):
        wb = openpyxl.load_workbook(self.path1)
        ws = wb['Sheet2']
        fileList = []
        for root, dirs, files in os.walk(self.path2):
            # 循环遍历列表：files【所有文件】，仅得到不包含路径的文件名
            for fileObj in files:
                # 空列表写入遍历的文件名称，兵勇目录路径拼接文件名称
                fileList.append(os.path.join(root, fileObj))

        list_col = ['A', 'B', "C", 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', "M", 'N', 'O', 'P', "Q", "R", 'S', 'T',
                    'U', 'V', 'W', 'X', 'Y', "Z"]
        num2 = len(fileList)
        print(fr'共计生成{num2}张二维码')
        n = num2 // 26
        num3 = 0
        for col in list_col[:n + 1]:
            for path, i in zip(fileList[:], range(1, 27)):
                num3 += 1
                print(num3)
                fileList.remove(path)
                img = openpyxl.drawing.image.Image(path)
                img.width = 40
                img.height = 40
                ws.row_dimensions[i].height = 45
                ws.row_dimensions[i].height = 45
                ws.add_image(img, fr'{col}{i}')
        wb.save('打印表.xlsx')
        wb.close()


if __name__ == "__main__":
    start = time.time()
    path = '/Users/user/PycharmProjects/text1/已完成工具例子区/打印二维码/CYC打码.xlsx'
    AE = Re_port(path)
    AE.get_png()
    AE.get_excel()
    print("Please start your creation")
    print(f"此程序运行是时间为{round(time.time() - start, 2)}秒")